﻿using System;
using System.Data;
using System.IO;

namespace WebApiCoreDemo.Common
{
    public static class DataRowExt
    {
        public static T ToModel<T>(this DataRow row)
        {
            //获取泛型的真实类型
            Type t = typeof(T);
            //实例化T类型的变量
            T model = (T)Activator.CreateInstance(t);
            //获取对象中的属性
            var props = t.GetProperties();
            foreach (var prop in props)
            {
                if (row.Table.Columns.Contains(prop.Name))
                {
                    prop.SetValue(model, row[prop.Name]);
                }
            }
            return model;
        }
    }
}
